package com.coffeeloversupportlibrary.coffeelover.domain.explor;

import android.util.Log;

import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.coffeelover.domain.explor.entity.Dialog;
import com.coffeeloversupportlibrary.coffeelover.domain.explor.entity.DialogOrigin;
import com.coffeeloversupportlibrary.coffeelover.domain.explor.entity.HomeOrigin;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/29 10:22
 * @projectName CoffeeLoverSupportLibrary
 * @className ExploreModel.java
 * @description TODO
 */
public class ExploreModel extends BaseModel {

    public ExploreModel(IDataRepository dataRepository) {
        super(dataRepository);
    }

    public void openDialog(int type, ModelCallBack<Dialog,String> callBack){
        Log.e("PUYV","Model 层调用仓库层网络服务");
        getRetrofitService(ApiService.class)
                .startDialog(type)
                .enqueue(new Callback<HomeOrigin>() {
                    @Override
                    public void onResponse(Call<HomeOrigin> call, Response<HomeOrigin> response) {
                         if(response.body().getCode()==20000){
                             Dialog dialog=new Dialog(response.body().getData().getCommend().getId(),
                                     response.body().getData().getCommend().getDialogueContent());
                             callBack.onSuccess(dialog);
                         }
                    }

                    @Override
                    public void onFailure(Call<HomeOrigin> call, Throwable t) {
                        // TODO: 2021/11/29 展示抛出信息
                          callBack.Failure(t.getMessage());
                    }
                });
    }

    public void dialog(int id,ModelCallBack<List<Dialog>,String> callBack){
        getRetrofitService(ApiService.class)
                .dialog(id)
                .enqueue(new Callback<DialogOrigin>() {
                    @Override
                    public void onResponse(Call<DialogOrigin> call, Response<DialogOrigin> response) {
                            if(response.body().getCode()==20000){
                                callBack.onSuccess(dataFunction(response.body().getData().getCommend()));
                            }
                    }

                    @Override
                    public void onFailure(Call<DialogOrigin> call, Throwable t) {
                            callBack.Failure(t.getMessage());
                    }
                });

    }

    private List<Dialog> dataFunction(List<DialogOrigin.DataDTO.CommendDTO> source){
        List<Dialog> target=new ArrayList<>();
        for(DialogOrigin.DataDTO.CommendDTO dto:source){
            Dialog dialog=new Dialog(dto.getId(),dto.getDialogueContent());
            target.add(dialog);
        }
        return target;
    }
}
